package com.vm.fragment;

import java.util.List;
import com.vm.db.DownInfoDao;
import com.vm.model.ActiveSoft;
import com.vm.model.DownInfo;
import com.vm.myview.ViewDownUtils;
import com.wwtscapp.apk.R;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

@SuppressLint("InflateParams")
public class DownloadingFragment extends BaseFragment {
	private View view;
	ListView lv_downloading;
	DownInfoDao infoDao;
	ViewDownUtils viewDownUtils;
	ActiveSoft activeSoft;
	LinearLayout found_ll_1;
	/** 标志位，标志已经初始化完成 */
	private boolean isPrepared;
	/** 是否已被加载过一次，第二次就不再去请求数据了 */
	private boolean mHasLoadedOnce;

	@Override
	protected void lazyLoad() {
		// TODO Auto-generated method stub
		if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}

		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				// 显示加载进度对话框
				mainhandler.sendMessage(new Message());
			}

			@Override
			protected Boolean doInBackground(Void... params) {
				try {
					Thread.sleep(300);
					// 在这里添加调用接口获取数据的代码

					// doSomething()
				} catch (Exception e) {
					e.printStackTrace();
				}
				return true;
			}

			@Override
			protected void onPostExecute(Boolean isSuccess) {
				if (isSuccess) {
					// 加载成功

					mHasLoadedOnce = true;
				} else {
					// 加载失败
				}
				// 关闭对话框
				// UIHelper.hideDialogForLoading();
			}
		}.execute();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.i("DownloadingFragment", "onCreateView");

		if (view == null) {
			view = inflater.inflate(R.layout.downloading, null);
		} else {
			// 缓存的rootView需要判断是否已经被加过parent
			// 如果有parent需要从parent删除
			ViewGroup parent = (ViewGroup) view.getParent();
			if (parent != null) {
				parent.removeView(view);
			}
		}
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		Log.i("DownloadingFragment", "onActivityCreated");

		isPrepared = true;
		lazyLoad();

	}

	public void init() {
		found_ll_1 = (LinearLayout) view.findViewById(R.id.found_ll_1);
		infoDao = new DownInfoDao(getActivity());
		viewDownUtils = new ViewDownUtils(getActivity());
		List<DownInfo> list = infoDao.getDownloading();
		if (list != null) {
			int i = 0;
			for (DownInfo downInfo : list) {
				activeSoft = new ActiveSoft();
				activeSoft.setSoftId(downInfo.getSoftId());
				activeSoft.setName(downInfo.getName());
				activeSoft.setImageUrl(downInfo.getImageUrl());
				activeSoft.setApkUrl(downInfo.getApkUrl());
				found_ll_1.addView(viewDownUtils.dyAddView(i, activeSoft, 1));
				i++;
			}
		}
	}

	@SuppressLint("HandlerLeak")
	private Handler mainhandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			init();
		};
	};
}